<?php
error_reporting(0);
$isadmin=1;    
include '../comm/config.php';
include '../comm/msgset.php';
include '../comm/checkpostandget.php';
include 'check.php';

$id=$_POST['id'];

if($id=="")
{
    echo "错误：没有要结算的数据！";
    exit;
}

$q = "select * from ".$BIAOTOU."tradelist where Id=".$id;
//echo $q;
$rs=mysql_query($q);
$num = mysql_num_rows($rs);
//echo $num;
if(!$num)
{
	echo "错误：未找到该记录！";
	exit;
}
else
{
	$sql="select outer_code,commission,trade_id,fxje from ".$BIAOTOU."tradelist where id=".$id;
	$query=mysql_query($sql);
	$row=mysql_fetch_array($query);
	$outer_code=$row['outer_code'];
	$commission=$row['commission'];
	$trade_id=$row['trade_id'];
	$fxje=$row['fxje'];
	
	if($outer_code!=''){
					$sql="select ddusername,dengji from ".$BIAOTOU."user where Id=".$outer_code."";
					mysql_query("set names utf8");
					$rs=mysql_query($sql);
					//该代号的会员如果存在
					if(mysql_num_rows($rs)!=0)
					{
						while($rowuser = mysql_fetch_object($rs))
						{
							$ddusername=$rowuser->ddusername; 
							$dddengji=$rowuser->dengji; 
						}
						
						//取会员等级					
						$sj=date('Y-m-d H:i:s');
						
						//用户佣金
						$sql="update ".$BIAOTOU."user set `money`=`money`-'".$fxje."',dengji=dengji-1 where ddusername ='".$ddusername."'";
						mysql_query("set names utf8");
						mysql_query($sql);
						//用户消息
						$msg_deltrade=str_replace("[trade_id]",$trade_id,$msg_deltrade);
						$msg_deltrade=str_replace("[fxje]",$fxje,$msg_deltrade);
						$sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('一笔交易被取消！','".$msg_deltrade."','$sj',0,'".$ddusername."','网站客服');";
						mysql_query("set names utf8");
						mysql_query($sql);
						//用户明细
						$sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,addtime,memo) values ('".$ddusername."','交易取消',".-$fxje.",'$sj','交易号".$trade_id."')";
						mysql_query($sql);							
						//取推荐人
						$sql="select tjr from ".$BIAOTOU."user where ddusername='".$ddusername."' and tjr<>''";
						mysql_query("set names utf8");
						$t=mysql_query($sql);
						$num1 = mysql_num_rows($t);	
						//echo 	$sql;					
						if($num1!=0)
						{   
							$tjrid="";
							while ($tt = mysql_fetch_array($t)){
								$tjrid=$tt["tjr"]; //推广人佣金
								$sql="update ".$BIAOTOU."user set money=money-".round($commission*TGBL,2)." where Id='".$tjrid."'";
								mysql_query("set names utf8");
								mysql_query($sql);
								
								//取推荐人名
								$sqltjr="select ddusername from ".$BIAOTOU."user where Id='".$tjrid."'";
								$tgrq=mysql_query($sqltjr);
								while ($tu = mysql_fetch_array($tgrq)){
									$tjrusername=$tu["ddusername"]; }	
								
								//推广人明细
								$sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,addtime,memo) values ('".$tjrusername."','取消推广佣金',".-round($commission*TGBL,2).",'$sj','交易人".$ddusername."，交易号".$trade_id."')";
								mysql_query("set names utf8");
								mysql_query($sql);
								//推广人消息
								$tgje=round($commission*TGBL,2);
								$msg_deltradetuiguang=str_replace("[ddusername]",$ddusername,$msg_deltradetuiguang);
								$msg_deltradetuiguang=str_replace("[trade_id]",$trade_id,$msg_deltradetuiguang);
								$msg_deltradetuiguang=str_replace("[tgje]",$tgje,$msg_deltradetuiguang);
								$sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('取消推广佣金！','".$msg_deltradetuiguang."','$sj',0,'".$tjrusername."','网站客服');";
								mysql_query($sql);
							}
						}
					}				
				}
	
	$q = "delete from ".$BIAOTOU."tradelist where id=".$id;
	mysql_query($q);
}
     
admin_log('删除淘宝订单');
mysql_close();
header('Location: tradelist.php');
?>
